local baidu = fk.CreateSkill {
  name = "th_baidu",
  max_phase_use_time = 2,
}

baidu:addEffect("active", {
  name = "th_baidu",
  anim_type = "control",
  max_card_num = 2,
  card_filter = function(self, player, to_select, selected)
    return #selected < 2
  end,
  target_num = 1,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  interaction = function()
    return UI.ComboBox{ choices = {"+", "-"} }
  end,
  prompt = function(self, player, selected_cards, selected_targets)
    if self.interaction.data then
      local n = #selected_cards + 1
      local sym = table.indexOf({"+", "-"}, self.interaction.data)
      local prompts = {"#th_baidu-plus", "#th_baidu-minus"}
      return Fk:translate("#th_baidu-prompt"):gsub("%%str", Fk:translate(prompts[sym])) .. ":::" .. n
    end
    return ""
  end,
  on_use = function(self, room, effect)
    local from = effect.from
    local to = effect.tos[1]
    local x = #effect.cards + 1
    if x > 1 then
      room:throwCard(effect.cards, baidu.name, from, from)
    end
    room:addTableMark(from, "_th_baidu", {to.id, x})
    if self.interaction.data == "-" then
      room:setPlayerMark(to, "@th_baidu", to:getMark("@th_baidu") - x)
      local card = room:askToChooseCards(from, {
        target = to,
        min = x,
        max = x,
        flag = "he",
        skill_name = baidu.name
      })
      if card then
        room:throwCard(card, baidu.name, to, from)
      end
    else
      room:setPlayerMark(to, "@th_baidu", to:getMark("@th_baidu") + x)
      to:drawCards(x, baidu.name)
    end
  end
}, { check_skill_limit = true })

local lose = {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("_th_baidu") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, marks in ipairs(player:getTableMark("_th_baidu")) do
      local p, n = room:getPlayerById(marks[1]), marks[2]
      if p:getMark("@th_baidu") ~= 0 then
        room:setPlayerMark(p, "@th_baidu", math.max(p:getMark("@th_baidu") - n, 0))
      end
    end
    room:setPlayerMark(player, "_th_baidu", 0)
  end
}
baidu:addEffect(fk.BuryVictim, lose)
baidu:addEffect(fk.TurnStart, lose)
baidu:addEffect("distance", {
  correct_func = function(self, from, to)
    return from:getMark("@th_baidu")
  end
})

Fk:loadTranslationTable{
  ["th_baidu"] = "摆渡",
  [":th_baidu"] = '出牌阶段限两次，你可以指定一名角色，选择是否弃置至多两张牌并选择一项（X为你本次弃置牌数+1）：<br>' ..
  '1.其与其他角色的距离+X并摸等量张牌；<br>' ..
  '2.其与其他角色的距离-X，你弃置其等量张牌。<br>' ..
  "你下回合开始或死亡时，重置以此法变动的距离。",

  ["#th_baidu-prompt"] = "摆渡：令一名角色与其他角色的距离%str",
  ["#th_baidu-plus"] = "+%%arg并令其摸%%arg张牌",
  ["#th_baidu-minus"] = "-%%arg并弃置其%%arg张牌",

  ["@th_baidu"] = "摆渡",
}

return baidu